package com.qikux.filter;

import com.qikux.utils.ContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 将请求和响应 放入到 某一个容器中，可以允许请求 在 项目的任何位置都能使用
 */
@WebFilter("/*")
public class ContextFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request ;
        HttpServletResponse resp = (HttpServletResponse)response;
        ContextUtils.setRequestAndResponse(req, resp);

        // chain 将请求交给下一个链
        chain.doFilter(request, response);

        // 在请求结束后,移除 request和response
        ContextUtils.removeRequestAndResponse();
    }
}
